package com.dyna.mapper;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dyna.core.LambdaQueryWrapperX;
import com.dyna.domain.dto.LoginUser;
import com.dyna.domain.resp.PageResult;
import com.dyna.foundation.domain.bo.UserInfoBo;
import com.dyna.foundation.domain.entity.SysUser;
import com.dyna.foundation.domain.param.UserParam;
import com.dyna.foundation.domain.req.SysUserPageReq;
import com.dyna.foundation.domain.resp.SysUserResp;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.Collection;
import java.util.List;

/**
* @author Admin
* @description 针对表【user】的数据库操作Mapper
* @createDate 2023-08-13 20:16:58
*/

@Mapper
public interface SysUserMapper extends BaseMapperX<SysUser> {

    default Long selectCount() {
        return selectCount(new QueryWrapper<>());
    }

    default SysUser selectByUsername(String username) {
        return selectOne(SysUser::getUsername, username);
    }

    default SysUser selectByPhone(String phone) {
        return selectOne(SysUser::getPhone, phone);
    }

    default SysUser queryUserByUsername(String userName) {
        LambdaQueryWrapper<SysUser> queryWrapperX = new LambdaQueryWrapper<>();
        queryWrapperX.select(
                SysUser::getUserId,
                SysUser::getUsername,
                SysUser::getNickName,
                SysUser::getPassword,
                SysUser::getSex,
                SysUser::getStatus,
                SysUser::getPhone,
                SysUser::getIdCard
        );
        queryWrapperX.eq(StrUtil.isNotBlank(userName), SysUser::getUsername, userName);
        return selectOne(queryWrapperX);
    }

    default List<SysUser> selectListByStatus(Integer status) {
        return selectList(SysUser::getStatus, status);
    }

    default PageResult<SysUser> selectPage(SysUserPageReq reqVO, Collection<Long> deptIds, Collection<Long> userIds) {
        return selectPage(reqVO, new LambdaQueryWrapperX<SysUser>()
                .likeIfPresent(SysUser::getUsername, reqVO.getUsername())
                .likeIfPresent(SysUser::getPhone, reqVO.getMobile())
                .eqIfPresent(SysUser::getStatus, reqVO.getStatus())
                .betweenIfPresent(SysUser::getCreateTime, reqVO.getCreateTime())
                .inIfPresent(SysUser::getDeptId, deptIds)
                .inIfPresent(SysUser::getUserId, userIds)
                .orderByDesc(SysUser::getUserId));
    }

    /**
     * 用户列表分页查询
     * @param userParam req
     */
    List<SysUserResp> queryPageList(@Param("req") UserParam userParam);

    /**
     * 查询用户名
     * @param username 用户名
     * @return LoginUser
     */
    LoginUser queryUserInfoByUsername(@Param("username") String username);

    /**
     * 根据用户id查询登录用户信息
     * @param userId 用户id
     * @return LoginUser
     */
    LoginUser queryLoginUserById(@Param("userId") Long userId);

    /**
     * 根据用户id查询用户信息
     * @param userId 用户id
     * @return UserInfoBo
     */
    UserInfoBo queryUserById(@Param("userId") Long userId);

    /**
     * 批量查询用户信息
     * @param uidList 用户ids
     * @return List<UserInfoBo>
     */
    List<UserInfoBo> queryUserByIds(@Param("uidList") List<Long> uidList);

    /**
     * 根据角色id查询用户信息
     * @param roleId 角色id
     * @return List<UserInfoBo>
     */
    List<UserInfoBo> queryUserByRoleId(@Param("roleId") Long roleId);

}




